<template>
  <div class="wrap_menu">
    <el-form ref="formData" :rules="rules" :model="formData" class="form-wrap" label-position="top" label-width="80px">
      <el-form-item :label="$t('configLazyLoad.process.nodeName')" prop="DisplayName">
        <el-input v-model.trim="formData.DisplayName"></el-input>
      </el-form-item>
      <el-form-item>
        <span style="font-size: 12px;color: #000;margin-right: 16px;">菜单节点类型：</span>
        <el-radio-group v-model="formData.MenuType" @change="turnIvr">
          <el-radio label="inner">转内部IVR</el-radio>
          <el-radio label="out">转外部IVR</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="IVR转向：" v-show="isInner">
        <node-select-item :curPbx="curPbx" :ToPeerType="formData.NextPeerType" :ToPeer="formData.NextPeer" ref="nodeSelect"></node-select-item>
      </el-form-item>
      <el-form-item label="外部号码：" v-show="!isInner">
        <el-input v-model.trim="formData.MenuOutNum"></el-input>
      </el-form-item>
    </el-form>
    <div class="note">
      <div class="wrap_text" v-show="isInner">
        <p>注意事项:</p>
        <p>转内部 IVR 验证， 不会把来电与第三方系统接通，但是需要采集来电的输入信息，通过 URL 节点来调用第三方系统的接口。 并且把采集的信息传递过去，然后把接口返回的结果显示给座席。</p>
        <p class="two">配制方法:</p>
        <p>1. 在验证过程的这一系列节点中，必须有一个是为 “URL 节点”， 在 URL 节点中取得的值赋给系统变量 “VAL_OF_IVR_MENU”，这样座席才能收到验证效果。</p>
        <p>2. 在这一系列节点的最后一定要配置一个自定义节点（作为最后一个节点）， 自定义节点的内容为： s => { goto ivr-menu-end, s, 1; }</p>
      </div>
      <div class="wrap_text" v-show="!isInner">
        <p>转外部 IVR 验证，会把来电与第三方系统接通。 座席听音乐等待验证，当第三方系统挂机，将来电转回与座席继续通话。 转外部 IVR 菜单，验证结束后不会返回给座席验证的结果。</p>
      </div>
    </div>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import nodeSelectItem from '../../base/nodeSelectItem.vue'
  import {deepClone} from 'src/utils/m7Utils.js'
  export default {
    name: 'menuDeal',
    data () {
      return {
        formData: { // ivr节点值
          _id: '',
          DisplayName: '',
          MenuType: 'inner',
          NextPeerType: '',
          NextPeer: '',
          MenuOutNum: ''
        },
        isInner: true, // 内部外部切换控制
        rules: {
          DisplayName: [
            { required: true, message: this.$t('configLazyLoad.process.processLabel7'), trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      },
      Account () {
        return this.$store.state.session.user.account
      }
    },
    components: {
      nodeSelectItem
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {
            _id: '',
            DisplayName: '',
            MenuType: 'inner',
            NextPeerType: '',
            NextPeer: '',
            MenuOutNum: ''
          }
        }
      },
      dealType: String
    },
    methods: {
      initForm (obj) {
        this.formData = deepClone(obj)
        if (this.formData['MenuType'] === 'out') {
          this.isInner = false
        } else {
          this.isInner = true
        }
      },
      // 重置表单
      resetForm () {
        this.$refs['formData'].resetFields()
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      // 更新pbx数据
      updateVuexPbx () {
        this.$store.dispatch('queryIvrMenu', {Account: this.Account, PBX: this.curPbx}).then(res => {
          if (res && res.success) {
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_ivr_menu', data: res.list || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      },
      saveClick () {
        this.$refs['formData'].validate((valid) => {
          if (valid) {
            let form = deepClone(this.formData)
            form.menuType = form.MenuType
            delete form.MenuType
            if (!form.id) {
              form.Account = this.Account
              form.PBX = this.curPbx
            }
            if (form.menuType === 'inner') {
              delete form.MenuOutNum
            }
            form.NextPeerType = this.$refs.nodeSelect.ToPeerTypeThis
            form.NextPeer = this.$refs.nodeSelect.ToPeerThis
            form.UpdateTime && delete form.UpdateTime
            this.$store.dispatch('updateIvrMenu', form).then((res) => {
              if (res.success) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.updateVuexPbx()
              } else {
                this.$message.error('保存失败')
              }
            })
          }
        })
      },
      // 菜单节点类型切换
      turnIvr (val) {
        this.isInner = val === 'inner'
      }
    },
    beforeMount () {
      this.initForm(this.form)
    },
    watch: {
      form (cur, old) {
        this.$refs['formData'].resetFields()
        this.initForm(cur)
        this.$refs.nodeSelect.init()
      }
    }
  }
</script>
<style lang="stylus" scoped>
  @import "../../../../../../assets/common.styl"
  .note
    padding 0 20px 20px
    .wrap_text
      padding 20px
      background: $c-back
    p
      color $c-prompt
    p.two
      margin-top 10px
  .wrap_menu
    width 100%
    padding-bottom 60px
    .form-wrap
      padding 20px
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
